Imports System.Text.RegularExpressions

Public Class RegexTest

    Private Sub btnMatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMatch.Click

        tvwMatches.Nodes.Clear()

        Dim pattern As String = txtPattern.Text
        Dim options As RegexOptions
        If chkSingleLine.Checked Then
            options = options Or RegexOptions.Singleline
        End If
        Dim input As String = txtInput.Text

        Dim re As New Regex(pattern, options)
        Dim mc As MatchCollection = re.Matches(input)
        Dim mi As Integer = 0
        For Each m As Match In mc
            With tvwMatches.Nodes.Add(mi, "Match " & mi & " at " & m.Index & " (length = " & m.Length & "): """ & m.Value & """")
                Dim gc As GroupCollection = m.Groups
                Dim gi As Integer = 0
                For Each g As Group In gc
                    Dim gn As String = re.GroupNameFromNumber(gi)
                    If gn Is Nothing OrElse gn.Length = 0 Then
                        gn = gi
                    End If
                    With .Nodes.Add(gi, "Group " & gn & " at " & g.Index & " (length = " & g.Length & "): """ & g.Value & """")
                        Dim cc As CaptureCollection = g.Captures
                        Dim ci As Integer = 0
                        For Each c As Capture In cc
                            .Nodes.Add(ci, "Capture " & ci & " at " & c.Index & " (length = " & c.Length & "): """ & c.Value & """")
                            ci += 1
                        Next
                    End With
                    gi += 1
                Next
            End With
            mi += 1
        Next
    End Sub
End Class